//
//  fenLeiController.m
//  YuErmuYing
//
//  Created by Tiancheng on 16/9/12.
//  Copyright © 2016年 Tiancheng. All rights reserved.
//

#import "fenLeiController.h"
#import "HttpRequest.h"
#import "yiYuanDuoBaoViewController.h"
#import "fenLei.h"
#import "customLeftTableViewCell.h"
#import "customRightTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "Macros.h"
#import "goodsDetailViewController.h"
#import "yiYuanDuoBaoNextView.h"
#import "yiYuanDuoBao.h"

#import "SVProgressHUD.h"
@interface fenLeiController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *headerView;
@property (weak, nonatomic) IBOutlet UIView *headerView01;
@property (weak, nonatomic) IBOutlet UITableView *leftTableView;
@property (weak, nonatomic) IBOutlet UITableView *rightTtableView;

//左边的商品分类数组
@property(nonatomic,copy)NSMutableArray *typeArray;
//母婴产品数组
@property(nonatomic,copy)NSMutableArray *list_babyArray;
//家用电器
@property(nonatomic,copy)NSMutableArray *list_elsecArray;
//食品
@property(nonatomic,copy)NSMutableArray *list_foodArray;
//虚拟商品
@property(nonatomic,copy)NSMutableArray *list_norealArray;
//其他商品
@property(nonatomic,copy)NSMutableArray *list_otherArray;
//手机数码
@property(nonatomic,copy)NSMutableArray *list_phoneArray;
//总数组
@property(nonatomic,copy)NSMutableArray *allArray;
//确定出是哪个数组
@property(nonatomic,copy)NSArray *dataArray;
@property (nonatomic ,assign) NSInteger selectedIndex;
@property(nonatomic,retain)customLeftTableViewCell*cleftCell;
@end

@implementation fenLeiController
@synthesize allArray,dataArray,typeArray,list_babyArray,list_elsecArray,list_foodArray,list_norealArray,list_otherArray,list_phoneArray,rightTtableView;
-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden=YES;
    self.tabBarController.tabBar.hidden=YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //设置背景色
    [self setBackGroundColor];
    dataArray=[NSMutableArray array];
    typeArray=[NSMutableArray array];
    list_babyArray=[NSMutableArray array];
    list_elsecArray=[NSMutableArray array];
    list_foodArray=[NSMutableArray array];
    list_norealArray=[NSMutableArray array];
    list_otherArray=[NSMutableArray array];
    list_phoneArray=[NSMutableArray array];

    [self getDataFromNet];
//    self.leftTableView.delegate=self;
//    self.leftTableView.dataSource=self;
//    self.rightTtableView.delegate=self;
//    self.rightTtableView.dataSource=self;
    [SVProgressHUD showWithStatus:@"加载中请稍后..."];
    self.leftTableView.hidden=YES;
    self.rightTtableView.hidden=YES;
}
//设置背景色
-(void)setBackGroundColor
{
        self.headerView.backgroundColor=[UIColor colorWithRed:244/255.0 green:142/255.0 blue:177/255.0 alpha:1];
        self.headerView01.backgroundColor=[UIColor colorWithRed:240/255.0 green:98/255.0 blue:146/255.0 alpha:1];
}
//从服务器获取数据
-(void)getDataFromNet
{
    NSString*urlString=[NSString stringWithFormat:@"http://112.74.114.1/Parenting/index.php/api/indiana/typelist"];
    HttpRequest *httpRequest=[HttpRequest sharedInstance];
    [httpRequest postWithURLString:urlString parameters:nil success:^(id responseObject)
     {
         id json = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
         NSDictionary *dic_data=[json objectForKey:@"data"];
         typeArray=[dic_data objectForKey:@"type"];
         list_phoneArray=[dic_data objectForKey:@"手机数码"];
         list_babyArray=[dic_data objectForKey:@"母婴产品"];
         list_foodArray=[dic_data objectForKey:@"食品"];
         list_norealArray=[dic_data objectForKey:@"虚拟商品"];
         list_elsecArray=[dic_data objectForKey:@"家用电器"];
         list_otherArray=[dic_data objectForKey:@"其他商品"];
//         NSLog(@"list_phoneArray=%@",list_phoneArray);
//         NSLog(@"list_babyArray=%@",list_babyArray);
//         NSLog(@"list_foodArray=%@",list_foodArray);
//         NSLog(@"list_norealArray=%@",list_norealArray);
//         NSLog(@"list_elsecArray=%@",list_elsecArray);
//         NSLog(@"list_otherArray=%@",list_otherArray);

//         //手机数码,母婴产品,食品,虚拟商品,家用电器,其他商品
         allArray=[NSMutableArray arrayWithObjects:list_otherArray,list_babyArray,list_foodArray,list_norealArray,list_phoneArray,list_elsecArray, nil];
//         NSLog(@"allArray.count=%ld",allArray.count);
         if (allArray.count>0)
         {
//             self.leftTableView.delegate=self;
//             self.leftTableView.dataSource=self;
//             self.rightTtableView.delegate=self;
//             self.rightTtableView.dataSource=self;
//             [self.leftTableView reloadData];
//             [self.rightTtableView reloadData];
             [self dismiss];
         }
//         NSLog(@"json=%@",json);
//         NSLog(@"dic_data=%@",dic_data);
     } failure:^(NSError *error) {
         NSLog(@"error=%@",error);
     }];
}
- (void)dismiss
{
    [SVProgressHUD dismiss];
    self.leftTableView.delegate=self;
    self.leftTableView.dataSource=self;
    self.rightTtableView.delegate=self;
    self.rightTtableView.dataSource=self;
    [self.leftTableView reloadData];
    [self.rightTtableView reloadData];
    self.leftTableView.hidden=NO;
    self.rightTtableView.hidden=NO;
}
//实现TableView的代理方法;
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([tableView isEqual:self.leftTableView])
    {
        return typeArray.count;
    }
    else if ([tableView isEqual:self.rightTtableView])
    {
        dataArray=allArray[self.selectedIndex];
//        NSLog(@"allArray=%@",allArray);
//        NSLog(@"allArray[self.selectedIndex]=%@",allArray[self.selectedIndex]);
        //dataArray=<null>
//        NSLog(@"dataArray=%@",dataArray);
        return dataArray.count;
    }
    return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([tableView isEqual:self.leftTableView])
    {
        static NSString *identifier = @"myCell";
        customLeftTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell)
        {
            cell=[[[NSBundle mainBundle] loadNibNamed:@"customLeftTableViewCell" owner:self options:nil]lastObject];
            if (indexPath.row==self.selectedIndex)
            {
                cell.leftLabel.textColor=pinkColor;
            }else{
                cell.leftLabel.textColor=[UIColor blackColor];
            }
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            cell.leftLabel.text=[typeArray[indexPath.row] objectForKey:@"indtype_name"];
        }
        return cell;
    }
else if ([tableView isEqual:rightTtableView])
    {
        static NSString *identifier = @"mycell";
        customRightTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        fenLei *fl=[fenLei shardeManager];
        if (!cell)
        {
            cell=[[[NSBundle mainBundle] loadNibNamed:@"customRightTableViewCell" owner:self options:nil]lastObject];
            //根据左边点击的indepath更新右边内容;
            NSMutableArray *mutableArray=allArray[_selectedIndex];
            fl.tidString=[mutableArray[indexPath.row] objectForKey:@"tid"];
            int num=[fl.tidString intValue];
            cell.joinButton.tag=num;
//            NSLog(@"fl.tidString=%@",fl.tidString);
            fl.involeString=[mutableArray[indexPath.row] objectForKey:@"invole"];
            fl.sumString=[mutableArray[indexPath.row] objectForKey:@"sum"];
            fl.tnameString=[mutableArray[indexPath.row] objectForKey:@"tname"];
            fl.tpicString=[mutableArray[indexPath.row] objectForKey:@"tpic"];
            
            cell.nameLabel.text=fl.tnameString;
            cell.involveLabel.text=[NSString stringWithFormat:@"目前人数:%@",fl.involeString];
            cell.involveLabel.textColor=pinkColor;
            cell.sumLable.text=[NSString stringWithFormat:@"共需:%@",fl.sumString];
            cell.sumLable.textColor=pinkColor;
            [cell.joinButton addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
            //设置按钮的圆角半径不会被遮挡
            [cell.joinButton.layer setMasksToBounds:YES];
            //        [commitBtn.layer setCornerRadius:10];
            //设置边界的宽度
            [cell.joinButton.layer setBorderWidth:1];
            [cell.joinButton.layer setBorderColor:[UIColor colorWithRed:233/255.0 green:30/255.0 blue:99/255.0 alpha:1.0].CGColor];
            [cell.joinButton setTitleColor:pinkColor forState:UIControlStateNormal];
            //灰色的线
            float pink_x=cell.grayLine.frame.origin.x;
            float pink_y=cell.grayLine.frame.origin.y;
            float pink_height=cell.grayLine.frame.size.height;
            float involve=[fl.involeString intValue];
            float sum=[fl.sumString intValue];
            float pink_width=involve/sum*cell.grayLine.frame.size.width;
            UIView *pinkView=[[UIView alloc]initWithFrame:CGRectMake(pink_x, pink_y, pink_width, pink_height)];
            pinkView.backgroundColor=pinkColor;
            [cell.contentView addSubview:pinkView];
//            cell.selectionStyle
            NSURL* imagePath = [NSURL URLWithString:fl.tpicString];
//            NSLog(@"fl.tpicString=%@",fl.tpicString);
            //给一张默认图片，先使用默认图片，当图片加载完成后再替换
            [cell.picImageView sd_setImageWithURL:imagePath placeholderImage:[UIImage imageNamed:@"default"]];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
        }
        return cell;
    }
    return nil;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([tableView isEqual:self.leftTableView])
    {
        return 44;
    }
    else if ([tableView isEqual:self.rightTtableView])
    {
        return 130;
    }
    return 0;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];

    if ([tableView isEqual:self.leftTableView])
    {
        _selectedIndex = indexPath.row;
        [self.leftTableView reloadData];
        [self.rightTtableView reloadData];
    }
}
-(void)onClick:(UIButton*)sender
{
    NSString *str=[NSString stringWithFormat:@"%ld",sender.tag];
    yiYuanDuoBao*yydb=[yiYuanDuoBao shardeManager];
    yydb.idString=str;
    yiYuanDuoBaoNextView *yydbn=[yiYuanDuoBaoNextView shardeManager];
    yydbn.whichViewToNext=4;
    goodsDetailViewController *gdvc=[goodsDetailViewController new];
    [self.navigationController pushViewController:gdvc animated:YES];
}
- (IBAction)goBack:(id)sender
{
    yiYuanDuoBaoViewController *yydbvc=self.navigationController.viewControllers[1];
    [self.navigationController popToViewController:yydbvc animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
